package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IRelationshipRecordService;
import com.mdd.admin.validate.RelationshipRecordCreateValidate;
import com.mdd.admin.validate.RelationshipRecordUpdateValidate;
import com.mdd.admin.validate.RelationshipRecordSearchValidate;
import com.mdd.admin.vo.RelationshipRecordListedVo;
import com.mdd.admin.vo.RelationshipRecordDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.RelationshipRecord;
import com.mdd.common.mapper.RelationshipRecordMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 联系记录实现类
 * @author 香
 */
@Service
public class RelationshipRecordServiceImpl implements IRelationshipRecordService {
        
    @Resource
    RelationshipRecordMapper relationshipRecordMapper;

    /**
     * 联系记录列表
     *
     * @author 香
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<RelationshipRecordListedVo>
     */
    @Override
    public PageResult<RelationshipRecordListedVo> list(PageValidate pageValidate, RelationshipRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<RelationshipRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        relationshipRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:customerName@customer_name:str",
            "=:contactContent@contact_content:str",
            "=:contactWay@contact_way:str",
            "=:contactTypeId@contact_type_id:str",
            "=:contactType@contact_type:str",
            "like:creatorName@creator_name:str",
            "between:followUpTime@follow_up_time:str",
            "datetime:createTimeStart-createTimeEnd@create_time:str",
        });

        IPage<RelationshipRecord> iPage = relationshipRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<RelationshipRecordListedVo> list = new LinkedList<>();
        for(RelationshipRecord item : iPage.getRecords()) {
            RelationshipRecordListedVo vo = new RelationshipRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 联系记录详情
     *
     * @author 香
     * @param id 主键参数
     * @return RelationshipRecord
     */
    @Override
    public RelationshipRecordDetailVo detail(Integer id) {
        RelationshipRecord model = relationshipRecordMapper.selectOne(
                new QueryWrapper<RelationshipRecord>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        RelationshipRecordDetailVo vo = new RelationshipRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 联系记录新增
     *
     * @author 香
     * @param createValidate 参数
     */
    @Override
    public void add(RelationshipRecordCreateValidate createValidate) {
        RelationshipRecord model = new RelationshipRecord();
        model.setCustomerId(createValidate.getCustomerId());
        model.setCustomerName(createValidate.getCustomerName());
        model.setCustomerContactId(createValidate.getCustomerContactId());
        model.setCustomerContact(createValidate.getCustomerContact());
        model.setContactContent(createValidate.getContactContent());
        model.setContactTime(createValidate.getContactTime());
        model.setOurContactId(createValidate.getOurContactId());
        model.setOurContactName(createValidate.getOurContactName());
        model.setContactWay(createValidate.getContactWay());
        model.setContactTypeId(createValidate.getContactTypeId());
        model.setContactType(createValidate.getContactType());
        model.setCreatorId(createValidate.getCreatorId());
        model.setCreatorName(createValidate.getCreatorName());
        model.setFollowUpTime(createValidate.getFollowUpTime());
        model.setFileName(createValidate.getFileName());
        model.setFilePath(createValidate.getFilePath());
        model.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        relationshipRecordMapper.insert(model);
    }

    /**
     * 联系记录编辑
     *
     * @author 香
     * @param updateValidate 参数
     */
    @Override
    public void edit(RelationshipRecordUpdateValidate updateValidate) {
        RelationshipRecord model = relationshipRecordMapper.selectOne(
                new QueryWrapper<RelationshipRecord>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setCustomerId(updateValidate.getCustomerId());
        model.setCustomerName(updateValidate.getCustomerName());
        model.setCustomerContactId(updateValidate.getCustomerContactId());
        model.setCustomerContact(updateValidate.getCustomerContact());
        model.setContactContent(updateValidate.getContactContent());
        model.setContactTime(updateValidate.getContactTime());
        model.setOurContactId(updateValidate.getOurContactId());
        model.setOurContactName(updateValidate.getOurContactName());
        model.setContactWay(updateValidate.getContactWay());
        model.setContactTypeId(updateValidate.getContactTypeId());
        model.setContactType(updateValidate.getContactType());
        model.setCreatorId(updateValidate.getCreatorId());
        model.setCreatorName(updateValidate.getCreatorName());
        model.setFollowUpTime(updateValidate.getFollowUpTime());
        model.setFileName(updateValidate.getFileName());
        model.setFilePath(updateValidate.getFilePath());
        relationshipRecordMapper.updateById(model);
    }

    /**
     * 联系记录删除
     *
     * @author 香
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        RelationshipRecord model = relationshipRecordMapper.selectOne(
                new QueryWrapper<RelationshipRecord>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        relationshipRecordMapper.updateById(model);
    }

}
